﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public sealed class Test_Case43
	{
		private class TestRuntime : Runtime<RuntimeEnvironment>
		{
			public static Int32 SubsystemsToLoad;


			protected override RuntimeEnvironment CreateEnvironment(IRuntime runtime, IMessageBus messageBus)
			{
				return new RuntimeEnvironment(runtime, messageBus);
			}

			protected override void OnStartup()
			{
				for (Int32 i = 0; i < SubsystemsToLoad; i++)
					base.LoadSubsystem<TestSubsystem>();
			}
		}

		private class TestSubsystem : Subsystem<RuntimeEnvironment>
		{
			public static Int32 AgentsToSpawn;


			protected TestSubsystem(RuntimeEnvironment environment)
				: base(environment)
			{
			}

			protected override void OnLoad()
			{
				for (Int32 i = 0; i < AgentsToSpawn; i++)
					base.SpawnAgent<TestAgent>();
			}
		}

		private class TestAgent : Agent
		{
			protected TestAgent(AgentEnvironment environment, AgentOptions options)
				: base(environment, options)
			{
			}
		}


		// --- TESTS ---


		private Int32 MinWorkerThreads;
		private Int32 MinCompletionThreads;


		[SetUp]
		public void Setup()
		{
			TestRuntime.SubsystemsToLoad = 1;
			TestSubsystem.AgentsToSpawn = 1;

			ThreadPool.GetMinThreads(out MinWorkerThreads, out MinCompletionThreads);
			ThreadPool.SetMinThreads(1, MinCompletionThreads);
		}

		[TearDown]
		public void Cleanup()
		{
			ThreadPool.SetMinThreads(MinWorkerThreads, MinCompletionThreads);
		}

		[Test]
		public void Test_Single_Runtime_Subsystem_Agent()
		{
			var watch = new Stopwatch();

			using (var runtime = new TestRuntime())
			{
				runtime.Start();

				watch.Start();
				Helper.Dispose(runtime);
				watch.Stop();
			}

			Console.WriteLine(watch.Elapsed.TotalMilliseconds);
			Assert.That(watch.Elapsed.TotalMilliseconds, Is.LessThan(500));
		}

		[Test]
		public void Test_Single_Runtime_Subsystem_1000_Agents()
		{
			TestSubsystem.AgentsToSpawn = 1000;

			var watch = new Stopwatch();

			using (var runtime = new TestRuntime())
			{
				runtime.Start();

				watch.Start();
				Helper.Dispose(runtime);
				watch.Stop();
			}

			Console.WriteLine(watch.Elapsed.TotalMilliseconds);
			Assert.That(watch.Elapsed.TotalMilliseconds, Is.LessThan(500));
		}

		[Test]
		public void Test_Single_Runtime_1000_Subsystem_1_Agents()
		{
			TestRuntime.SubsystemsToLoad = 1000;

			var watch = new Stopwatch();

			using (var runtime = new TestRuntime())
			{
				runtime.Start();

				watch.Start();
				Helper.Dispose(runtime);
				watch.Stop();
			}

			Console.WriteLine(watch.Elapsed.TotalMilliseconds);
			Assert.That(watch.Elapsed.TotalMilliseconds, Is.LessThan(500));
		}

	}
}

#endif
